package com.canbot.u05.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.utils.XunFeiYuYingUtils;

/**
 * 迎宾模式文字转语音语音录入对话框
 */

public class UsherModeXFListenerDialog extends Dialog {

        public final static int USHER_MODE = 1;

        public final static int HOST_MODE = 2;

        private Button btnFinish, btnCancle;

        private TextView recordTime;

        private ImageButton loading;

        private AnimationDrawable anim_Load;

        private int recLen = 0;

        private int minute = 0;

        final Handler handler = new Handler(Looper.getMainLooper());

        private TextView viewById;

        private IXunFeiResult xunFeiResult;

        public UsherModeXFListenerDialog(Context context, int type) {
                super(context, R.style.MyDialogStyle);
                if (type == USHER_MODE) {
                        setCustomDialog();
                }
                else if (type == HOST_MODE) {
                        setHostModeCustomDialog();
                }

        }

        private void setCustomDialog() {
                View mView = LayoutInflater.from(getContext()).inflate(R.layout.layout_usher_mode_record_speech_dialog, null);
                recordTime = (TextView) mView.findViewById(R.id.record_time);
                viewById = (TextView) mView.findViewById(R.id.tip);
                btnFinish = (Button) mView.findViewById(R.id.record_dialog_finish);
                btnCancle = (Button) mView.findViewById(R.id.record_dialog_cancle);
                loading = (ImageButton) mView.findViewById(R.id.vLoad);
                loading.setBackgroundResource(R.drawable.usher_mode_record_load_anim);
                anim_Load = (AnimationDrawable) loading.getBackground();
                super.setContentView(mView);
        }

        /**
         * 主持模式录音框
         */
        private void setHostModeCustomDialog() {
                View mView = LayoutInflater.from(getContext()).inflate(R.layout.layout_host_mode_record_speech_dialog, null);
                recordTime = (TextView) mView.findViewById(R.id.record_time);
                btnFinish = (Button) mView.findViewById(R.id.record_dialog_finish);
                btnCancle = (Button) mView.findViewById(R.id.record_dialog_cancle);
                loading = (ImageButton) mView.findViewById(R.id.vLoad);
                loading.setBackgroundResource(R.drawable.host_mode_record_load_anim);
                anim_Load = (AnimationDrawable) loading.getBackground();
                anim_Load.start();
                setContentView(mView);
        }

        public void setTime(String str) {
                viewById.setText(str);
                btnFinish.setEnabled(false);
                btnCancle.setEnabled(false);
        }

        public void show(IXunFeiResult xunFeiResult) {
                super.show();
                this.xunFeiResult = xunFeiResult;
                btnFinish.setEnabled(true);
                btnCancle.setEnabled(true);
                anim_Load.start();
                handler.postDelayed(runnable, 1000);
                startRecorder();
        }

        Runnable runnable = new Runnable() {
                @Override
                public void run() {
                        recLen++;
                        if (recLen > 59) {
                                recLen = 0;
                                minute++;
                                if (recLen < 10) {
                                        recordTime.setText("" + minute + ":0" + recLen);
                                }
                                else {
                                        recordTime.setText("" + minute + ":" + recLen);
                                }
                        }
                        else {
                                if (recLen < 10) {
                                        recordTime.setText("" + minute + ":0" + recLen);
                                }
                                else {
                                        recordTime.setText("" + minute + ":" + recLen);
                                }
                        }
                        handler.postDelayed(this, 1000);
                }
        };

        @Override
        public void setCancelable(boolean flag) {
                super.setCancelable(false);
        }

        @Override
        public void cancel() {
                super.cancel();
        }

        public void ok() {
                stopRecorder();
                minute = 0;
                recLen = 0;
                recordTime.setText("" + minute + ":0" + recLen);
                handler.removeCallbacks(runnable);
                cancel();
        }

        public void no() {
                cancel();
                stopRecorder();
                minute = 0;
                recLen = 0;
                recordTime.setText("" + minute + ":0" + recLen);
                handler.removeCallbacks(runnable);
        }


        /**
         * 确定键监听器
         *
         * @param listener
         */
        public void setFinishClickListener(View.OnClickListener listener) {
                btnFinish.setOnClickListener(listener);
        }

        /**
         * 取消键监听器
         *
         * @param listener
         */
        public void setCancleClickListener(View.OnClickListener listener) {
                btnCancle.setOnClickListener(listener);
        }

        /**
         * 录音时长
         */
        public void getRecordTime() {
                recordTime.getText();
        }

        private void startRecorder() {
                XunFeiYuYingUtils.getInstance().startRecognize(new XunFeiYuYingUtils.ResultListenr() {
                        @Override
                        public void onResult(String result) {
                                if (xunFeiResult != null) {
                                        xunFeiResult.onResult(result);
                                }
                        }

                        @Override
                        public void onError(int result) {
                                if (xunFeiResult != null) {
                                        xunFeiResult.onError(result);
                                }
                        }

                        @Override
                        public void onVolumeChanged(int i) {

                        }

                        @Override
                        public void onBeginOfSpeech() {

                        }

                        @Override
                        public void onEndOfSpeech() {
                                no();
                        }
                });
        }

        private void stopRecorder() {
                XunFeiYuYingUtils.getInstance().stopRecognize();
        }

        public interface IXunFeiResult {

                void onResult(String result);

                void onError(int result);
        }
}
